import request from '@/utils/request'

export function iotMqttSubscribe(data: { topic: string }) {
  return request({ url: '/iot/mqtt/subscribe', method: 'post', data })
}

export function iotMqttPublish(data: { topic: string; payload: string }) {
  return request({ url: '/iot/mqtt/publish', method: 'post', data })
}

export function iotListRecords(params: any) {
  return request({ url: '/iot/data/records', method: 'get', params })
}

// Modbus 测试
export function modbusTcpRead(params: { slaveId: number; offset: number; deviceId?: number; productId?: number; host?: string; port?: number; timeoutMs?: number }) {
  return request({ url: '/iot/modbus/tcp/readHolding', method: 'get', params })
}
export function modbusRtuRead(params: { slaveId: number; offset: number; deviceId?: number; productId?: number; portName?: string; baudRate?: number; dataBits?: number; stopBits?: number; parity?: number; timeoutMs?: number }) {
  return request({ url: '/iot/modbus/rtu/readHolding', method: 'get', params })
}

// Modbus 写寄存器
export function modbusTcpWrite(params: { slaveId: number; offset: number; value: number; deviceId?: number; productId?: number; host?: string; port?: number; timeoutMs?: number }) {
  return request({ url: '/iot/modbus/tcp/writeHolding', method: 'post', params })
}
export function modbusRtuWrite(params: { slaveId: number; offset: number; value: number; deviceId?: number; productId?: number; portName?: string; baudRate?: number; dataBits?: number; stopBits?: number; parity?: number; timeoutMs?: number }) {
  return request({ url: '/iot/modbus/rtu/writeHolding', method: 'post', params })
}

// 设备分页列表（用于映射选择设备）
export function iotDevicePage(params: any) {
  return request({ url: '/iot/device/page', method: 'get', params })
}

// 设备管理 CRUD
export function iotDeviceGet(id: number) {
  return request({ url: `/iot/device/${id}`, method: 'get' })
}
export function iotDeviceCreate(data: any) {
  return request({ url: '/iot/device', method: 'post', data })
}
export function iotDeviceUpdate(data: any) {
  return request({ url: '/iot/device', method: 'put', data })
}
export function iotDeviceRemove(id: number) {
  return request({ url: `/iot/device/${id}`, method: 'delete' })
}

// 设备分组管理
export function iotGroupPage(params: any) {
  return request({ url: '/iot/device-group/page', method: 'get', params })
}
export function iotGroupGet(id: number) {
  return request({ url: `/iot/device-group/${id}`, method: 'get' })
}
export function iotGroupCreate(data: any) {
  return request({ url: '/iot/device-group', method: 'post', data })
}
export function iotGroupUpdate(data: any) {
  return request({ url: '/iot/device-group', method: 'put', data })
}
export function iotGroupRemove(id: number) {
  return request({ url: `/iot/device-group/${id}`, method: 'delete' })
}
export function iotGroupListDevices(groupId: number) {
  return request({ url: `/iot/device-group/${groupId}/devices`, method: 'get' })
}
export function iotGroupSetDevices(groupId: number, deviceIds: number[]) {
  return request({ url: `/iot/device-group/${groupId}/devices`, method: 'post', data: deviceIds })
} 

// 远程控制相关 API
export function controlMqttPublish(data: { topic: string; payload: string; deviceId?: number; deviceCode?: string }) {
  return request({ url: '/iot/control/mqtt/publish', method: 'post', data })
}

export function controlModbusWrite(data: { protocol?: 'TCP'|'RTU'; slaveId: number; offset: number; value: number; deviceId?: number; deviceCode?: string }) {
  return request({ url: '/iot/control/modbus/writeHolding', method: 'post', data })
}

export function controlRecords(params: { deviceId?: number; deviceCode?: string; channel?: string; action?: string; status?: string; beginTime?: string; endTime?: string; pageNum?: number; pageSize?: number }) {
  return request({ url: '/iot/control/records', method: 'get', params })
} 